import {
  get,
  getById,
  getLikes
} from "../../utils/db"
const app = getApp()
let openid = wx.getStorageSync('openid')
Page({
  data: {
    activeIndex: 0,
    isLogin: true, //是否登录。 false 未登录  true，已经登录
    userInfo: {}, //用户信息
    recipes: [], //用户菜单
    typeList: [], //用户添加的菜谱分类列表
    types: [
      "../../static/type/type01.jpg",
      "../../static/type/type02.jpg",
      "../../static/type/type03.jpg",
      "../../static/type/type04.jpg",
      "../../static/type/type05.jpg",
      "../../static/type/type06.jpg",
    ],
    lists: [],
    page: 1,
    pageSize: 10,
  },
  onLoad() {
    this.getMyUserInfo();
    this.getMyMenu();
    if (this.data.userInfo) {
      app.globalData.isLogin = true
    } else {
      app.globalData.isLogin = false
    }
  },
  onShow() {
    this.getLikesList();
    this.getMyMenu()
  },
  // 获取用户菜单
  async getMyMenu() {
    // let res=await get("menu",{
    //   _openid:openid
    // }).catch(err=>{
    //   console.log(err);
    //   return;
    // })
    // this.setData({
    //   recipes:res.data
    // })
    var page = this.data.page; //第几页
    var pageSize = this.data.pageSize; //每一页显示几条
    var _skip = (page - 1) * pageSize; //跳过多少条

    var result = await db.collection("menu").skip(_skip).limit(pageSize).orderBy("addtime", "desc").get()
        .catch(err => console.error(err))
    this.setData({
        recipes: this.data.recipes.concat(result.data)
    })

  },
  // 获取用户信息
  getMyUserInfo() {
    let userInfo = app.globalData.userInfo
    if (userInfo == null) {
      this.setData({
        isLogin: false
      })
      app.getInfo = res => {
        this.setData({
          userInfo: res,
          isLogin: true
        })
      }
    } else {
      this.setData({
        isLogin: true,
        userInfo
      })
    }
  },
  _delStyle() {
    wx.showModal({
      title: "删除提示",
      content: "确定要删除么？",

    })
  },
  getUserInfo(e) {
    let userInfo = e.detail.userInfo
    if (userInfo != undefined) {
      this.setData({
        isLogin: true,
        userInfo
      })
    }
    this.getMyMenu();
  },
  myClick() {
    wx.navigateTo({
      url: '/pages/pbrecipe/pbrecipe',
    })
  },
  select(e) {
    let activeIndex = e.currentTarget.id
    this.setData({
      activeIndex
    })
    if (activeIndex == 1) {
      this.getTypeList();
    } else if (activeIndex == 2) {
      this.getLikesList()
    }
  },
  // 跳转分类列表修改页
  toCategory() {
    wx.navigateTo({
      url: '/pages/category/category',
    })
  },
  // 获取分类列表
  async getTypeList() {
    let res = await get("type", {
      _openid: openid
    }).catch(err => console.error(err));
    this.setData({
      typeList: res.data
    })
  },
  // 跳转分类列表
  toCateList(e) {
    wx.navigateTo({
      url: '/pages/list/list?id=' + e.currentTarget.id + '&name=' + e.currentTarget.dataset.name + '&openid' + openid,
    })
  },
  // 关注列表
  async getLikesList() {
    let res = await get("likes", {
      _openid: openid
    }).catch(err => console.error(err));
    let menuids = res.data.map(item => {
      return item.menuid
    })
    let res1 = await getLikes("menu", menuids).catch(err => console.error(err));
    this.setData({
      lists: res1.data
    })
  },
  // 跳转详情页面
  toDetail(e) {
    wx.navigateTo({
      url: '/pages/detail/detail?id=' + e.currentTarget.id,
    })
  },
})